#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Version: Python 2.6.6
# Author: Sergey K.
# Created: 2011-11-04


import unittest

import rs.core


class CoreTest(unittest.TestCase):

    def setUp(self):
        rs.core.registry.clear()

    def tearDown(self):
        rs.core.registry.clear()

    def test_has_get_set_rs(self):
        def test(): pass
        true = self.assertTrue
        true(not hasattr(test, '__rs_dict__'))
        true(not rs.core.has_rs(test))
        rs.core.set_rs(test, object())
        true(hasattr(test, '__rs_dict__'))
        true(rs.core.has_rs(test))
        true(getattr(test, '__rs_dict__') is rs.core.get_rs(test))

    def test_path(self):
        true = self.assertTrue
        false = self.assertFalse
        eq = self.assertEquals
        def a(): pass
        false(rs.core.has_rs(a))
        false(a in rs.core.registry)
        rs.core.path('//A\\B/\\')(a)
        true(rs.core.has_rs(a))
        true(a in rs.core.registry)
        d = rs.core.get_rs(a)
        eq('/A\\B', d.path)
        
    def test_method(self):
        true = self.assertTrue
        false = self.assertFalse
        eq = self.assertEquals
        def a(): pass
        false(rs.core.has_rs(a))
        false(a in rs.core.registry)
        rs.core.method('HEAD')(a)
        true(rs.core.has_rs(a))
        true(a in rs.core.registry)
        d = rs.core.get_rs(a)
        eq('HEAD', d.method)

    def test_produces(self):
        true = self.assertTrue
        false = self.assertFalse
        eq = self.assertEquals
        def a(): pass
        false(rs.core.has_rs(a))
        false(a in rs.core.registry)
        rs.core.produces('application/json', 2)(a)
        true(rs.core.has_rs(a))
        false(a in rs.core.registry)
        d = rs.core.get_rs(a)
        eq(('application/json', 2), d.producer)
        
    def test_consumes(self):
        true = self.assertTrue
        false = self.assertFalse
        eq = self.assertEquals
        def a(): pass
        false(rs.core.has_rs(a))
        false(a in rs.core.registry)
        rs.core.consumes('application/json', 1)(a)
        true(rs.core.has_rs(a))
        false(a in rs.core.registry)
        d = rs.core.get_rs(a)
        eq(('application/json', 1), d.consumer)


if __name__ == '__main__':
    unittest.main()